package com.yzy.soft.system.controller;

import com.google.common.collect.Lists;
//import com.sinosoft.liscw.system.service.ISpecialtreeService;
import com.yzy.soft.common.persistence.model.Specialtree;
import com.yzy.soft.common.persistence.dao.SpecialtreeMapper;
import com.yzy.soft.system.dto.TableDataDto;
import com.yzy.soft.core.base.controller.BaseController;

import com.yzy.soft.core.base.tips.ErrorTip;
import com.yzy.soft.core.base.tips.Tip;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.apache.ibatis.session.RowBounds;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.util.Date;
import java.util.List;

/**
* 描述：专项控制层
* @author Yinzhiyuan
* @date 2018-06-18 23:58:39
*/
@Api(value = "专项Controller",description = "专项控制接口")
@RestController
@RequestMapping("/specialtree")
public class SpecialtreeController extends BaseController{

//    @Autowired
//    private ISpecialtreeService specialtreeService;
    
    @Autowired
    private SpecialtreeMapper specialtreeMapper;

    
    
    /**
    * 描述：查询 专项列表
    */
    @ApiOperation("查询专项")
    @ApiImplicitParams({@ApiImplicitParam(name="page",value="当前页数",dataType="int", paramType = "query"),@ApiImplicitParam(name="limit",value="页数据条数",dataType="int", paramType = "query")})
    @GetMapping(value = "",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public TableDataDto getAll(@RequestParam(name = "page", defaultValue = "1", required = true) int page, @RequestParam(name = "limit", defaultValue = "10", required = true) int pageSize)throws Exception {
    
        Wrapper<Specialtree> specialtree=new EntityWrapper<Specialtree>();
        page=(page-1)*pageSize;
        pageSize=page+pageSize;
        List<Specialtree> result=specialtreeMapper.selectPage(new RowBounds(page,pageSize),specialtree);
        TableDataDto tableDataDto=new TableDataDto();
        tableDataDto.setCode("00");
        tableDataDto.setMsg("000");
        tableDataDto.setCount(specialtreeMapper.selectCount(specialtree));
        tableDataDto.setData(result);
        return tableDataDto;
    }

    /**
     * 描述：根据专项编号查询专项信息
     */
    @ApiOperation("根据专项编号查询专项信息")
    @GetMapping(value = "/{id}")//,produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    public Specialtree getById(@ApiParam(name="编码") @PathVariable("id") String id)throws Exception {
        return specialtreeMapper.selectById(id);
    }

    

    /**
    * 描述:创建专项
    * @param specialtree  专项DTO
    */
    @ApiOperation("创建专项")
    @PostMapping("")
    public Tip create(@ModelAttribute Specialtree specialtree) {
        specialtree.setCreate(new Date());
        specialtree.setUpdate(new Date());
        Integer result=specialtreeMapper.insert(specialtree);
        return SUCCESS_TIP;
    }

    /**
    * 描述：删除专项
    * @param id 专项id
    */
    @ApiOperation("删除专项")
    @DeleteMapping(value = "",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Tip deleteById(@ModelAttribute Specialtree specialtree) throws Exception {
        Integer result=specialtreeMapper.deleteById(specialtree.getId());
        return SUCCESS_TIP;
    }

    /**
    * 描述：更新专项
    * @param id 专项id
    */
    @ApiOperation("更新专项")
    @PutMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Tip updateSpecialtree(@ModelAttribute Specialtree specialtree) throws Exception {
        specialtree.setUpdate(new Date());
        Integer result=specialtreeMapper.updateById(specialtree);
        return SUCCESS_TIP;
    }

    @ApiOperation("批量将专项设置成弃用状态")
    @PostMapping("/batchNuUse")
    public Tip batchNuUse(@RequestParam(name="ids[]",required = false) String[] specialtree) {
        if(specialtree.length!=0){
            Integer result=specialtreeMapper.batchNuUseIds(Lists.newArrayList(specialtree) );
        }
        return SUCCESS_TIP;
    }
    @ApiOperation("批量将专项设置成可用状态")
    @PostMapping("/batchUse")
    public Tip batchUse(@RequestParam(name="ids[]",required = false) String[] specialtree) {
        if(specialtree.length!=0){
            Integer result=specialtreeMapper.batchUseIds(Lists.newArrayList(specialtree) );
        }
        return SUCCESS_TIP;
    }

//    @GetMapping("/accountId/{accountId}")
//    public TableDataDto getbyAccountId(@PathVariable(name="accountId") String accountId,@RequestParam(name="page",defaultValue = "1",required = true) int page, @RequestParam(name="limit",defaultValue = "10",required = true) int pageSize) {
//        Wrapper<Specialtree> expenseattr=new EntityWrapper<>();
//        expenseattr.where("accountid={0}",accountId);
//        page=(page-1)*pageSize;
//        pageSize=page+pageSize;
//        List<Specialtree> result=specialtreeMapper.selectPage(new RowBounds(page,pageSize),expenseattr);
//        TableDataDto tableDataDto=new TableDataDto();
//        tableDataDto.setCode("00");
//        tableDataDto.setMsg("000");
//        tableDataDto.setCount(specialtreeMapper.selectCount(expenseattr));
//        tableDataDto.setData(result);
//        return tableDataDto;
//    }


    @PutMapping("/status/{id}")
    public Tip status(@PathVariable("id")String id) {
        Integer result=specialtreeMapper.status(id);
        if(null==result){
            return new ErrorTip(500,"s");
        }
        return SUCCESS_TIP;
    }
    /**
     * 描述：查询 专项列表
     */
    @ApiOperation("查询专项-某科目设置专项")
    @ApiImplicitParams({@ApiImplicitParam(name="page",value="当前页数",dataType="int", paramType = "query")})
    @GetMapping(value = "/accountId/{accountId}")
    public List<Specialtree> getAccountIdAll(@PathVariable(name = "accountId") String accountId){
        List<Specialtree> tableDataDto=specialtreeMapper.selectByAccountId(accountId);
        return tableDataDto;
    }


}